import java.util.ArrayList;
import java.util.List;

public class Tree<K> {
    public TreeNode getRoot() {
        return root;
    }

    private TreeNode root;

    public Tree(TreeNode root) {
        this.root = root;
    }

    public Tree(K rootData) {
        root = new TreeNode(rootData);
    }

    public String toString() {
        return "Tree : \n" + root.toString(" ");
    }
}

class TreeNode<K> {
        private K data;
        private TreeNode parent;
        private List<TreeNode> children;

        public TreeNode(K rootData) {
            this.data = rootData;
            children = new ArrayList<TreeNode>();
        }

        public void addChild(TreeNode<K> node) {
            children.add(node);
        }

        public void removeChild(TreeNode<K> node) {
            for (TreeNode n : children)
                if (node.equals(node))
                    children.remove(n);
        }

        public String toString(String p) {
            StringBuilder prefix = new StringBuilder();
            prefix.append(p).append(data).append("\n");
                for (TreeNode n : children) {
                    if (n != null) {
                        prefix.append(n.toString(p + " "));
                    }
                }
            return prefix.toString();
        }
    }
